const express = require('express');
const router = express.Router();
const db = require('../db');

//获取分类
router.get('/list',(req,res)=>{
    db(`select * from category`,(err,result)=>{
        if(err) throw err;
        res.send({
            status:0,
            message:'获取分类成功',
            data:result 
        })
    })
    // res.send('1111')
})

//添加分类
router.post('/add',(req,res)=>{

    let {name,alias} = req.body;
    
    db(`insert into category set name="${name}",alias="${alias}"`,(err,result)=>{
        if(err) throw err;
        res.send({
            status:1,
            message:'添加分类成功'
        })
    })
})

//删除分类
router.get('/delete',(req,res)=>{

    // console.log(req.query.id);

    db(`delete from category where id=${req.query.id}`,(err,result)=>{
        if(err) throw err;
        res.send({
            status:0,
            message:'删除分类成功'
        })
    })
})

//修改分类
router.post('/update',(req,res)=>{

    let {id,name,alias} = req.body;

    db(`update category set name="${name}",alias="${alias}" where id=${id}`,(err,result)=>{
        if(err) throw err;
        res.send({
            status:0,
            message:'修改分类成功'
        })
    })
})

module.exports= router;